<template>
  <el-form label-width="150px" ref="formRef" :model="dataForm" :rules="dataRule">
    <el-card class="box-card" shadow="never">
      <template #header>
        <div class="card-header">
          <span>{{ i18n('question.question_info') }}</span>
        </div>
      </template>
      <div>
        <el-form-item class="input" :label="i18n('question.title')" prop="title">
          <el-input type="textarea" v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('question.title')"/>
        </el-form-item>
      </div>
    </el-card>

    <el-card class="box-card mt10" shadow="never">
      <template #header>
        <div class="card-header">
          <span>{{ i18n('question.answer_info') }}</span>
        </div>
      </template>
      <div>
        <el-form-item class="input" v-for="(option, index) in dataForm.options" :key="option.title" :prop="'options.' + index + '.value'">
          <el-row class="cx-width-100" :gutter="5">
            <el-col :span="2">
              <el-input v-model="option.title" :placeholder="i18n('common.please_input') + i18n('question.option.title')"/>
            </el-col>
            <el-col :span="22">
              <el-row :gutter="5">
                <el-col :span="14">
                  <el-input :height="60" v-model="option.value" :placeholder="i18n('common.please_input') + i18n('question.option.value')"/>
                </el-col>
                <el-col :span="6">
                  <el-input :height="60" v-model="option.url" :placeholder="i18n('common.please_input') + i18n('question.option.url')"/>
                </el-col>
                <el-col :span="2">
                  <el-button @click.prevent="removeOption(option)">
                    {{ i18n('common.delete') }}
                  </el-button>
                </el-col>
                <el-col :span="2">
                  <el-button type="success" @click.prevent="addOption()">
                    {{ i18n('question.option.create') }}
                  </el-button>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </el-form-item>
      </div>
    </el-card>

    <el-card class="box-card mt10" shadow="never">
      <div class="text item">
        <el-form-item>
          <el-button
            type="primary"
            @click="dataFormSubmit(formRef)">
            {{ i18n('common.confirm') }}
          </el-button>
          <el-button @click="resetForm(formRef)">
            {{ i18n('common.reset') }}
          </el-button>
        </el-form-item>
      </div>
    </el-card>
  </el-form>
</template>

<script setup name="question-choices-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { isEmpty } from '@/utils/validate'

  const { router, query, i18n, isAuth } = useBase()

  const props = defineProps({
    model: {
      type: String,
      default: null,
    }
  });

  const dataForm = reactive({
    id: '',
    type: 1,
    title: '',
    level: 2,
    score: 5,
    options: [
      { title: 'A', value: '', url: '' },
      { title: 'B', value: '', url: '' },
      { title: 'C', value: '', url: '' },
      { title: 'D', value: '', url: '' },
    ],
    answer: [],
    analysis: '',
  })

  const dataRule = reactive({
    title: [{
      required: true,
      message: i18n('question.rules.title.require'),
      trigger: 'blur'
    }],
    options: [{
      required: true,
      message: i18n('question.rules.options.require'),
      trigger: 'blur'
    }],
  })

  const {
    formRef,
    resetForm
  } = useForm(props.model, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${props.model}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.title = data.data.title
            dataForm.level = data.data.level
            dataForm.score = data.data.score
            dataForm.options = data.data.options
            dataForm.answer = data.data.answer
            dataForm.analysis = data.data.analysis
          }
        })
      }
    })
  }

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl(`/${props.model}/choices`),
          method: 'post',
          data: $http.adornData(dataForm),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const addOption = () => {
    // 校验
    if (dataForm.options.length > 0) {
      let option = dataForm.options[dataForm.options.length - 1]
      if (isEmpty(option.title)) {
        $message.error(i18n('question.placeholder_input_option'));
        return
      }
      dataForm.options.push({ title: '', value: '', url: '' })
    } else {
      dataForm.options.push({ title: '', value: '', url: '' })
    }
  }

  const removeOption = (item) => {
    let index = dataForm.options.indexOf(item)
    if (index !== -1) {
      dataForm.options.splice(index, 1)
    }
  }

  onMounted(() => {
    init(formRef)
  })
</script>
